{% extends 'index.html' %}
{% block subtitle %}- 购物车{% endblock %}
<!--head-->
{% block head %}
    <style type="text/css">
        td {
            margin-left: 40px;
        }

        span {
            margin-left: 20px;
        }
    </style>
    <script type="text/javascript">
        function send_yes_order_to_order() {
            var data = {
                data: JSON.stringify({
                    "code": 111,
                }),
            }
            $.ajax({
                url: "/order/",
                type: "post",
                data: data,
                datatype: "json",
                success: function (data) {
                    if (data['code'] === 200) {
                        alert("订单提交成功")
                        window.location.href = "{{ url_for('main.shopping_car') }}"
                    } else if (data['code'] === 407) {
                        alert("无购物车条目，没有创建订单")
                    } else if (data['code'] === 409) {
                        temp_str = ""
                        for(var i in data['count_not_full_GoodsName_list']){
                            temp_str += "  " + data['count_not_full_GoodsName_list'][i]["count_not_full_GoodsName"]
                        }
                        alert("商品库存不足，无法购买！库存不足商品为："+temp_str)
                    } else if (data['code'] === 411) {
                        alert("用户金额不足，无法购买！")
                    }
                    else {
                        alert("未知错误")
                    }
                },
                error: function (error) {
                    console.log(error)
                    alert("出现未知错误")
                }
            })
        }

        function delete_shopping_car(BuyCarID) {
            document.getElementById(BuyCarID).remove();
            var data = {
                data: JSON.stringify({
                    "BuyCarID": BuyCarID,
                }),
            }
            $.ajax({
                url: "/delete_shopping_car/",
                type: "post",
                data: data,
                datatype: "json",
                success: function (data) {
                    if (data['code'] === 200) {
                        alert("删除购物车成功！")
                    } else {
                        alert("不知道什么错误！")
                    }
                },
                error: function (error) {
                    console.log(error)
                    alert("未知错误")
                }
            })
        }
    </script>
{% endblock %}

{% block content %}
    <div id="main">
        <table>
            <tr>
                <td>商品名称</td>
                <td>单价</td>
                <td>数量</td>
                <td>金额</td>
                <td>操作</td>
            </tr>
            {% for one_buycar_data in buycar_list %}
                <tr id="{{ one_buycar_data['BuyCarID'] }}">
                    <td>{{ one_buycar_data['GoodsName'] }}</td>
                    <td>{{ one_buycar_data['GoodsPrice'] }}</td>
                    <td>{{ one_buycar_data['BuyCarNumber'] }}</td>
                    <td>{{ one_buycar_data['TotalPrice'] }}</td>
                    <td>
                        <button onclick="delete_shopping_car({{ one_buycar_data['BuyCarID'] }})">删除</button>
                    </td>
                </tr>
            {% endfor %}
            {% if buycar_list %}
                <tr>
                    <td colspan='5'><input type="button" class="input_button" value="结账" id="sub_btn"
                                           onclick="send_yes_order_to_order()"></td>
                </tr>
            {% endif %}
        </table>
    </div>
{% endblock %}